import 'package:flutter/material.dart';

class EventDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _EventDemoState();
  }
}

class _EventDemoState extends State {

  //定义一个状态，保存当前指针位置
  PointerEvent _event;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('指针事件的例子'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            // 打印事件信息
            Listener(
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.all(10),
                color: Colors.blue,
                width: 300.0,
                height: 150.0,
                child: Text(_event?.toString()??"",style: TextStyle(color: Colors.white)),
              ),
              onPointerDown: (PointerDownEvent event) => setState(()=>_event=event),
              onPointerMove: (PointerMoveEvent event) => setState(()=>_event=event),
              onPointerUp: (PointerUpEvent event) => setState(()=>_event=event),
            ),

            // 扩大点击区域
            Listener(
                child: ConstrainedBox(
                  constraints: BoxConstraints.tight(Size(300.0, 150.0)),
                  child: Center(child: Text("扩大点击区域", style:TextStyle(backgroundColor: Colors.red) ,)),
                ),
                behavior: HitTestBehavior.opaque,
                onPointerDown: (event) => print("扩大点击区域")
            ),

            // 点透
            Stack(
              children: <Widget>[
                Listener(
                  child: ConstrainedBox(
                    constraints: BoxConstraints.tight(Size(300.0, 200.0)),
                    child: DecoratedBox(
                        decoration: BoxDecoration(color: Colors.blue)),
                  ),
                  onPointerDown: (event) => print("底层蓝色区域"),
                ),
                Listener(
                  child: ConstrainedBox(
                    constraints: BoxConstraints.tight(Size(200.0, 100.0)),
                    child: Center(child: Text("左上角200*100范围内非文本区域点击")),
                  ),
                  onPointerDown: (event) => print("上层文字"),
                  behavior: HitTestBehavior.translucent, //放开此行注释后可以"点透"
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}